package com.travel.server.manager.impl.command.customer;

import com.travel.common.CommonConstant;
import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dto.request.customer.QueryCustomerInfoListRequest;
import com.travel.server.manager.dto.response.customer.QueryCustomerInfoListResponse;
import com.travel.server.manager.dto.response.customer.QueryCustomerInfoListResponse.CustomerItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.*;
import com.travel.server.manager.impl.dal.CustomerDal.CustomerParam;
import com.travel.server.manager.impl.dal.CustomerTagDal.CustomerTagParam;
import com.travel.server.manager.impl.dal.MemberGradeDal.MemberGradeParam;
import com.travel.server.manager.impl.dal.TagDal.TagParam;
import com.travel.server.manager.model.CustomerInfo;
import com.travel.server.manager.model.CustomerTagInfo;
import com.travel.server.manager.model.MemberGradeInfo;
import com.travel.server.manager.model.TagInfo;
import com.travel.server.manager.model.dto.CustomerItemDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/9 18:02
 * @Desc:
 */
@Component
@Slf4j
public class QueryCustomerInfoListCommand extends AbstractCommand<QueryCustomerInfoListRequest, QueryCustomerInfoListResponse> {

    @Autowired
    private CustomerDal customerDal;

    @Autowired
    private MemberGradeDal memberGradeDal;

    @Autowired
    private TagDal tagDal;

    @Autowired
    private CustomerTagDal customerTagDal;

    @Override
    protected void checkInput(QueryCustomerInfoListRequest request) {
        checkPageInput(request);
    }

    @Override
    public QueryCustomerInfoListResponse handle(QueryCustomerInfoListRequest request) {
        checkPageInput(request);
        QueryCustomerInfoListResponse response = new QueryCustomerInfoListResponse();

        CustomerParam param = makeParam(request);
        if(param == null){
            return response;
        }

        PageDto<CustomerItemDto> pageDto = customerDal.selectByPage(param);

        return makeResponse(pageDto);
    }

    public List<CustomerInfo> selectExcelData(QueryCustomerInfoListRequest request){
        CustomerParam param = makeParam(request);
        if(param == null){
            return null;
        }
        return customerDal.selectByExample(param);
    }

    private QueryCustomerInfoListResponse makeResponse(PageDto pageDto){
        QueryCustomerInfoListResponse response = new QueryCustomerInfoListResponse();
        response.setTotalNum(pageDto.getTotal());
        List<CustomerItem> customerList = new ArrayList<>();
        List<CustomerItemDto> list = pageDto.getList();
        if(!CollectionUtil.isEmpty(list)){
            for(CustomerItemDto customerItemDto : list){
                CustomerItem customerItem = new CustomerItem();
                customerItem.setId(customerItemDto.getId());
                customerItem.setMail(customerItemDto.getMail());
                customerItem.setMemberGrade(customerItemDto.getMemberGrade());
                customerItem.setMobile(customerItemDto.getMobile());
                customerItem.setName(customerItemDto.getName());
                customerItem.setSource(customerItemDto.getSource());
                customerItem.setWechatNick(customerItemDto.getWechatNick());
                customerItem.setWechatNumber(customerItemDto.getWechatNumber());
                customerItem.setMemberGradeId(customerItemDto.getMemberGradeId());
                customerList.add(customerItem);
            }
        }

        response.setCustomerList(customerList);
        return response;
    }

    private CustomerParam makeParam(QueryCustomerInfoListRequest request){
        CustomerParam param = new CustomerParam();
        if(request.getIsMember() != null){
            param.setIsMember(request.getIsMember());
        }
        if(StringUtils.isNotBlank(request.getMobile())){
            param.setMobile(request.getMobile());
        }
        if(StringUtils.isNotBlank(request.getName())){
            param.setName(request.getName());
        }
        if(StringUtils.isNotBlank(request.getWechatNumber())){
            param.setWechatNumber(request.getWechatNumber());
        }
        if(StringUtils.isNotBlank(request.getSource())){
            param.setSource(request.getSource());
        }
        if(StringUtils.isNotBlank(request.getMemberGrade())){
            //查会员等级id
            MemberGradeParam memberGradeParam = new MemberGradeParam();
            memberGradeParam.setName(request.getMemberGrade());
            List<MemberGradeInfo> list = memberGradeDal.selectByExample(memberGradeParam);
            if(CollectionUtil.isEmpty(list)){
                return null;
            }
            List<Integer> memberGradeIdList = new ArrayList<>();
            for(MemberGradeInfo memberGrade : list){
                memberGradeIdList.add(memberGrade.getId());
            }
            param.setMemberGradeIdList(memberGradeIdList);
        }
        if(StringUtils.isNotBlank(request.getTag())){
            //查标签id
            TagParam tagParam = new TagParam();
            tagParam.setName(request.getTag());
            List<TagInfo> tagList = tagDal.selectByExample(tagParam);
            if(CollectionUtil.isEmpty(tagList)){
                return null;
            }
            List<Integer> tagIdList = new ArrayList<>();
            for(TagInfo tagInfo : tagList){
                tagIdList.add(tagInfo.getId());
            }
            //查标签对应关系
            CustomerTagParam customerTagParam = new CustomerTagParam();
            customerTagParam.setTagIdList(tagIdList);
            List<CustomerTagInfo> customerTagList = customerTagDal.selectByExample(customerTagParam);
            if(CollectionUtil.isEmpty(customerTagList)){
                return null;
            }
            List<Integer> customerIdList = new ArrayList<>();
            for(CustomerTagInfo customerTagInfo : customerTagList){
                customerIdList.add(customerTagInfo.getCustomerId());
            }
            param.setIdList(customerIdList);
        }
        param.setLimit(request.getLimit());
        param.setOffset(request.getOffset());
        return param;
    }
}
